package com.flyqiu.form.core.rule.valid;

import cn.hutool.core.convert.Convert;
import com.alibaba.fastjson2.JSONObject;
import com.flyqiu.form.api.rule.FlyQiuDataPropValidate;
import com.flyqiu.form.api.rule.FlyQiuFormDataRule;
import com.flyqiu.form.api.rule.FlyQiuValidateContext;
import com.flyqiu.form.api.tools.FlyQiuAvueColTools;
import com.flyqiu.form.common.enums.FlyQiuFormDataAction;
import com.flyqiu.form.common.exceptions.FormDataValidateException;
import com.flyqiu.form.model.entity.FlyQiuTableConfig;
import org.apache.commons.lang3.StringUtils;

import java.math.BigDecimal;

public class MaxFlyQiuFormDataRule implements FlyQiuFormDataRule {
    /**
     * 数据项必填标识
     */
    public static final String KEY = "max";


    @Override
    public String getLabel() {
        return "最大";
    }

    @Override
    public String getValue() {
        return KEY;
    }


    @Override
    public FlyQiuDataPropValidate createDataValidate(JSONObject column, JSONObject rule) {
        BigDecimal min = rule.getBigDecimal("max");
        if (min == null) {
            return null;
        }
        String message = rule.getString("message");
        if (StringUtils.isAllBlank(message)) {
            message = FlyQiuAvueColTools.getColumnLabel(column) + "不能小于" + min;
        }
        return new MaxValidate(message, min);
    }


    public static class MaxValidate implements FlyQiuDataPropValidate {
        private final String message;
        private final BigDecimal max;

        /**
         * 构造函数，初始化消息和最大值。
         *
         * @param message 验证失败时的消息
         * @param max     最大值
         */
        public MaxValidate(String message, BigDecimal max) {
            this.message = message;
            this.max = max;
        }

        @Override
        public void dataValidate(FlyQiuTableConfig config, JSONObject formData, Object data, FlyQiuFormDataAction action) {
            // 检查 data 是否为空
            if (data == null) {
                throw new FormDataValidateException(message);
            }

            // 将 data 转换为 BigDecimal
            BigDecimal convert = Convert.convert(BigDecimal.class, data);
            if (convert == null) {
                throw new FormDataValidateException(message);
            }

            // 比较大小
            if (convert.compareTo(max) > 0) {
                throw new FormDataValidateException(message);
            }
        }

        @Override
        public void dataValidate(FlyQiuValidateContext context, Object valData, FlyQiuFormDataAction action) {
            // 检查 valData 是否为空
            if (valData == null) {
                throw new FormDataValidateException("在【" + context.getTableConfig().getFormName() + "】第" + (context.getRowIndex() + 1) + " 行 " + message);
            }

            // 将 valData 转换为 BigDecimal
            BigDecimal convert = Convert.convert(BigDecimal.class, valData);
            if (convert == null) {
                throw new FormDataValidateException("在【" + context.getTableConfig().getFormName() + "】第" + (context.getRowIndex() + 1) + " 行 " + message);
            }

            // 比较大小
            if (convert.compareTo(max) > 0) {
                throw new FormDataValidateException("在【" + context.getTableConfig().getFormName() + "】第" + (context.getRowIndex() + 1) + " 行 " + message);
            }
        }
    }

}
